﻿// =================================================================== 
// 项目说明
//====================================================================
// 
// 文件： SYS_MAILINFO.cs
// 项目名称：


// 创建时间：2013/3/19
// 负责人：
// ===================================================================
using System;
using System.Data;
using CommonLib.Model;
using CommonLib.IDAL;
using CommonLib.DALFactory;

namespace CommonLib.BLL
{
    /// <summary>
    ///   的BLL层


    /// </summary>
    public class SYS_MAILINFO : SYS_MAILINFOBLLBase
    {
        #region ----------变量定义----------
        #endregion

        #region ----------构造函数----------
        /// <summary>
        /// 默认构造函数





        /// </summary>
        public SYS_MAILINFO()
        {
        }
        #endregion

        #region----------函数定义---------

        public static int AddTrans(CommonLib.Model.SYS_MAILINFO mailInfo_models, string[] user_id, int Draft)
        {
            CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
            try
            {
                myTrans.BeginTransaction();
                int row = Add(myTrans, mailInfo_models);
                if (row > 0)
                {
                    CommonLib.Model.SYS_MAILOWNER mailOwer_models = new CommonLib.Model.SYS_MAILOWNER();
                    mailOwer_models.MAILID = mailInfo_models.MAILID;
                    mailOwer_models.USER_ID = mailInfo_models.SENDUSERID;
                    if (Draft == 1)//存草稿箱
                    {
                        mailOwer_models.FOLDERFLAG = 3;
                        mailOwer_models.READFLAG = 0;
                        mailOwer_models.DELETEFLAG = 0;
                        SYS_MAILOWNER.Add(myTrans, mailOwer_models);
                    }
                    else  //发送邮件
                    {
                        mailOwer_models.FOLDERFLAG = 2;
                        mailOwer_models.READFLAG = 1;
                        mailOwer_models.DELETEFLAG = 0;
                        SYS_MAILOWNER.Add(myTrans, mailOwer_models);//保存发送者自己



                        for (int i = 0; i < user_id.Length; i++)//保存发送对象
                        {
                            CommonLib.Model.SYS_MAILOWNER Model_MailOwner = new CommonLib.Model.SYS_MAILOWNER();
                            Model_MailOwner.MAILID = mailInfo_models.MAILID;
                            Model_MailOwner.USER_ID = user_id[i];
                            Model_MailOwner.FOLDERFLAG = 1;
                            Model_MailOwner.DELETEFLAG = 0;
                            Model_MailOwner.READFLAG = 0;
                            SYS_MAILOWNER.Add(myTrans, Model_MailOwner);
                        }
                    }
                }
                myTrans.CommitTran();
                return row;
            }
            catch (Exception ex)
            {
                string strEx = ex.Message;
                myTrans.RollBackTran();
                return 0;
            }
        }

        public static int UpdateTrans(CommonLib.Model.SYS_MAILINFO mailInfo_models, string[] user_id, int Draft)
        {
            CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
            try
            {
                myTrans.BeginTransaction();
                int row = Update(myTrans, mailInfo_models);
                if (row > 0)
                {
                    CommonLib.Model.SYS_MAILOWNER mailOwer_models = new CommonLib.Model.SYS_MAILOWNER();
                    mailOwer_models.MAILID = mailInfo_models.MAILID;
                    mailOwer_models.USER_ID = mailInfo_models.SENDUSERID;
                    if (Draft == 1)//存草稿箱
                    {
                        mailOwer_models.FOLDERFLAG = 3;
                        mailOwer_models.READFLAG = 0;
                        mailOwer_models.DELETEFLAG = 0;
                        SYS_MAILOWNER.Update(myTrans, mailOwer_models);
                    }
                    else  //发送邮件
                    {
                        CommonLib.BLL.SYS_MAILOWNER.DeleteFK(myTrans, "MailID=" + mailInfo_models.MAILID);
                        mailOwer_models.FOLDERFLAG = 2;
                        mailOwer_models.READFLAG = 1;
                        mailOwer_models.DELETEFLAG = 0;
                        CommonLib.BLL.SYS_MAILOWNER.Add(myTrans, mailOwer_models);

                        for (int i = 0; i < user_id.Length; i++)//保存发送对象
                        {
                            CommonLib.Model.SYS_MAILOWNER Model_MailOwner = new CommonLib.Model.SYS_MAILOWNER();
                            Model_MailOwner.MAILID = mailInfo_models.MAILID;
                            Model_MailOwner.USER_ID = user_id[i];
                            Model_MailOwner.FOLDERFLAG = 1;
                            Model_MailOwner.DELETEFLAG = 0;
                            Model_MailOwner.READFLAG = 0;
                            SYS_MAILOWNER.Add(myTrans, Model_MailOwner);
                        }
                    }
                }
                myTrans.CommitTran();
                return row;
            }
            catch (Exception ex)
            {
                string strEx = ex.Message;
                myTrans.RollBackTran();
                return 0;
            }
        }
        #endregion
    }
}
